package cn.wolfcode.util;

import cn.hutool.db.Page;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.service.INoticeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Component
public class UserContext {
    private UserContext(){}

    @Autowired
    private static INoticeService noticeService;

    public static final String  USER_IN_SESSION ="USER_IN_SESSION";
    public static final String  PERMISSION_IN_SESSION ="PERMISSION_IN_SESSION";
    public static final String  NOTICEMessage_IN_SESSION = "NOTICEMessage_IN_SESSION";


    public static HttpSession getSession(){
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession() ;
    }
    public static Employee getCurrentUser(){
        return (Employee)getSession().getAttribute(UserContext.USER_IN_SESSION);
    }


    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(UserContext.USER_IN_SESSION,employee);
    }
//    获取权限
    public static Permission getCurrentPermission(){
        return (Permission) getSession().getAttribute(UserContext.USER_IN_SESSION);
    }

//  设权限
    public static void setCurrentPermission(Permission permission){
        getSession().setAttribute(UserContext.USER_IN_SESSION,permission);
    }

    public static void setNoticeMessage(PageInfo pageInfo){
       getSession().setAttribute(UserContext.NOTICEMessage_IN_SESSION,pageInfo);
    }


    public static Map<String,Object> getNoticeMessage(){
        return ( Map<String,Object>)getSession().getAttribute(UserContext.NOTICEMessage_IN_SESSION);
    }





}
